﻿using UnityEngine;
using System.Collections;

namespace ChuMeng
{
    public class CameraMove : MonoBehaviour
    {
        float maxX = 9f;
        float minX = -12;
        float y = 2.9f;

        bool leftDown = false;
        bool gotPosition = false;
        Vector3 initPos;
        Vector3 initCameraPos;
        public float posScale = 0.01f;

        Vector3 targetPos;
        void Awake() {
        
        }

        // Use this for initialization
        void Start()
        {
            targetPos = transform.position;
        }

        void Update() {
            if(Input.GetMouseButtonDown(0)) {
                leftDown = true;
                initPos = Input.mousePosition;
                initCameraPos = transform.position;
            }
            if(Input.GetMouseButtonUp(0)) {
                leftDown = false;
            }
            if(leftDown) {
                //Debug.Log("mouse px "+Input.mousePosition);
                float diffX = (Input.mousePosition-initPos).x;
                targetPos = initCameraPos+ new Vector3(-diffX*posScale, 0, 0);
                targetPos.x = Mathf.Min(Mathf.Max(minX, targetPos.x), maxX);
            }

            transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime*5);
        }
    }
}
